package org.example.src.service.impl;

import org.example.src.dto.request.CompanyProfileRequest;
import org.example.src.dto.response.CompanyProfileResponse;
import org.example.src.entity.Company;
import org.example.src.entity.User;
import org.example.src.repository.CompanyRepository;
import org.example.src.repository.UserRepository;
import org.example.src.service.CompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<Company> findAllCompanies() {
        return companyRepository.findAll();
    }

    @Override
    public Company findCompanyById(Long id) {
        return companyRepository.findById(id).orElse(null);
    }

    @Override
    public Company findCompanyByCompanyName(String companyName) {
        return companyRepository.findByCompanyName(companyName).orElse(null);
    }

    @Override
    public Company createCompany(Company company) {
        return companyRepository.save(company);
    }

    @Override
    public Company updateCompany(Long id, Company companyDetails) {
        Company company = companyRepository.findById(id).orElseThrow(() -> new RuntimeException("公司未找到"));
        BeanUtils.copyProperties(companyDetails, company, "id");
        return companyRepository.save(company);
    }

    @Override
    public void deleteCompany(Long id) {
        companyRepository.deleteById(id);
    }

    @Override
    public List<Company> findCompaniesByApprovalStatus(Boolean isApproved) {
        return companyRepository.findByApprovalStatus(isApproved);
    }

    @Override
    public Company approveCompany(Long id) {
        Company company = companyRepository.findById(id).orElseThrow(() -> new RuntimeException("公司未找到"));
        company.setIsApproved(true);
        return companyRepository.save(company);
    }

    @Override
    public CompanyProfileResponse updateCompanyProfile(Long id, CompanyProfileRequest request) {
        Company company = companyRepository.findById(id).orElseThrow(() -> new RuntimeException("公司未找到"));

        company.setCompanyName(request.getCompanyName());
        company.setBusinessLicense(request.getBusinessLicense());
        company.setIndustry(request.getIndustry());
        company.setScale(request.getScale());
        company.setAddress(request.getAddress());
        company.setContactPerson(request.getContactPerson());
        company.setContactPhone(request.getContactPhone());
        company.setCompanyDescription(request.getCompanyDescription());

        Company updatedCompany = companyRepository.save(company);

        // 更新用户信息
        User user = userRepository.findById(id).orElse(null);
        if (user != null) {
            user.setEmail(updatedCompany.getEmail());
            userRepository.save(user);
        }

        CompanyProfileResponse response = new CompanyProfileResponse();
        BeanUtils.copyProperties(updatedCompany, response);
        if (user != null) {
            response.setUsername(user.getUsername());
            response.setEmail(user.getEmail());
        }

        return response;
    }
}
